import 'package:flutter/material.dart';
import 'package:heven/api/index.dart';
import 'package:heven/pages/set/widget/item.dart';
import 'package:heven/utils/index.dart';

class SetPage extends StatefulWidget {
  const SetPage({super.key, this.arguments});

  final arguments;

  @override
  State<SetPage> createState() => _SetPageState();
}

class _SetPageState extends State<SetPage> {
  late String _isTourist;
  @override
  void initState() {
    super.initState();
    setState(() {
      _isTourist = widget.arguments['isTourist'].toString();
    });
  }

  _remove() async {
    await Common.saveStorageValue('isTourist', '1');
    await Common.removeStorageValue('token');
    await Common.removeStorageValue('uuid');
    await Common.removeStorageValue('uid');
    await Common.saveStorageValue('isExit', '1');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('设置'),
      ),
      body: Column(
        children: [
          Container(
            height: 1,
            color: const Color(0xFF979797),
          ),
          ItemWidget(
            name: '隐私政策',
            onTap: () {
              Navigator.pushNamed(
                context,
                '/agree',
                arguments: {"name": "隐私政策", "type": "user_rule"},
              );
            },
          ),
          Container(
            height: 1,
            color: const Color(0xFF979797),
          ),
          ItemWidget(
            name: '用户协议',
            onTap: () {
              Navigator.pushNamed(
                context,
                '/agree',
                arguments: {"name": "用户协议", "type": "privacy_policy"},
              );
            },
          ),
          Container(
            height: 1,
            color: const Color(0xFF979797),
          ),
          Offstage(
            offstage: _isTourist == '1',
            child: Column(
              children: [
                ItemWidget(
                  name: '退出登录',
                  onTap: () async {
                    await _remove();

                    //是否退出登录过
                    Navigator.pushNamedAndRemoveUntil(
                        context,
                        '/home', // 你的首页路由名称
                        (route) => false, // 移除所有页面
                        arguments: {"index": 1});
                  },
                ),
                Container(
                  height: 1,
                  color: const Color(0xFF979797),
                ),
                ItemWidget(
                  name: '注销账号',
                  onTap: () async {
                    var uid = await Common.getStorageValue('uid');
                    await deleteUser({"uid": uid});
                    await _remove();

                    Navigator.pushNamedAndRemoveUntil(
                        context,
                        '/home', // 你的首页路由名称
                        (route) => false, // 移除所有页面
                        arguments: {"index": 1});
                  },
                ),
                Container(
                  height: 1,
                  color: const Color(0xFF979797),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
